package cn.tangcent;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by TangMing on 2017/4/9.
 */
public class Coordinate {
    private int x;
    private int y;

    private static Map<Integer, Coordinate> cache = new HashMap<>();

    private Coordinate(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public static Coordinate instance(int x, int y) {
        Coordinate coordinate;
        if ((coordinate = cache.get(SudokuUtils.toIndex(x, y))) == null) {
            coordinate = newInstance(x,y);
        }
        return coordinate;
    }

    private static synchronized Coordinate newInstance(int x, int y) {
        Coordinate coordinate;
        int index = SudokuUtils.toIndex(x, y);
        if ((coordinate = cache.get(index)) == null) {
            coordinate = new Coordinate(x, y);
            cache.put(index, coordinate);
        }
        return coordinate;
    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    public boolean not(int x, int y) {
        return x != this.x || y != this.y;
    }

    @Override
    public String toString() {
        return "[" +
                "x=" + x +
                ",y=" + y +
                ']';
    }
}
